package com.dbq.fast.business.login;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.business.login.bean.CustomProfile;
import com.dbq.fast.business.login.business.NewLoginBiz;
import com.dbq.fast.business.me.help.CountDownUtil;
import com.dbq.fast.common.base.BaseActivity;
import com.dbq.fast.util.PhoneFormatCheckUtils;
import com.dbq.fast.util.SystemUtil;
import com.dbq.fast.util.WindowToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.dbq.fast.business.login.NewLoginActivity.REGISTER;

public class ResetPwdActivity extends BaseActivity implements NewLoginBiz.LoginListener {

    public static final int VERIFY = 0x00;
    public static final int RESET_PWD = 0x01;

    @BindView(R.id.title)
    TextView title;
    // 获取验证码
    @BindView(R.id.phone_number)
    EditText phoneNumber;
    @BindView(R.id.access_verifi_code)
    TextView accessVerifiCode;
    @BindView(R.id.verification_code)
    EditText verificationCode;
    @BindView(R.id.check_verifi_code)
    Button checkVerifiCode;
    @BindView(R.id.switch_login)
    TextView switchLogin;
    @BindView(R.id.verification_code_area)
    RelativeLayout verifiCodeArea;
    // 重置密码
    @BindView(R.id.input_pwd)
    EditText inputPwd;
    @BindView(R.id.input_pwd_again)
    EditText inputPwdAgain;
    @BindView(R.id.bind_pwd)
    Button bindPwd;
    @BindView(R.id.bind_pwd_area)
    LinearLayout bindPwdArea;

    private int curWitch = REGISTER;
    private NewLoginBiz loginBiz;
    private CountDownUtil countDownUtil;
    private TextWatcher textWatcherPhone = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (charSequence.toString().trim().length() == 11) {
                accessVerifiCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_access_verifi_code_enable));
                accessVerifiCode.setAlpha(1);
                accessVerifiCode.setTextColor(getResources().getColor(android.R.color.white));
                accessVerifiCode.setEnabled(true);
            } else {
                accessVerifiCode.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_access_verifi_code_disable));
                accessVerifiCode.setAlpha(0.5f);
                accessVerifiCode.setTextColor(getResources().getColor(R.color.verifi_code_grey));
                accessVerifiCode.setEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_pwd);
        ButterKnife.bind(this);

        switchLogin.setVisibility(View.GONE);
        bindPwd.setText(getString(R.string.complete));
        loginBiz = new NewLoginBiz(this);
        loginBiz.setLoginListener(this);
        showWitch(VERIFY);
        initListener();

    }

    private void initListener() {
        phoneNumber.addTextChangedListener(textWatcherPhone);
    }

    public void showWitch(int witch) {
        curWitch = witch;
        accessVerifiCode.setVisibility(witch == VERIFY ? View.VISIBLE : View.GONE);
        verifiCodeArea.setVisibility(witch == VERIFY ? View.VISIBLE : View.GONE);
        bindPwdArea.setVisibility(witch == RESET_PWD ? View.VISIBLE : View.GONE);

        title.setText(witch == VERIFY ? getString(R.string.reset_pwd) : getString(R.string.title_set_new_pwd));
    }

    @OnClick({R.id.back, R.id.access_verifi_code, R.id.check_verifi_code, R.id.bind_pwd})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                onBackPressed();
                break;
            case R.id.access_verifi_code:
                accessVerifiCodeOp();
                break;
            case R.id.check_verifi_code:
                checkVerifiCodeOp();
                break;
            case R.id.bind_pwd:
                resetPwdOp();
                break;
        }
    }

    /**
     * 获取验证码
     */
    private void accessVerifiCodeOp() {
        if(!SystemUtil.isNetworkAvailable(App.getContext())){
            WindowToastUtils.showShort(App.getContext(), getString(R.string.tip_check_net_available));
            return;
        }
        verificationCode.requestFocus();
        String phoneNumberStr = phoneNumber.getText().toString().trim();
        boolean isPhoneLegal = PhoneFormatCheckUtils.isPhoneLegal(phoneNumberStr);
        if (isPhoneLegal) {
            accessVerifiCode.setEnabled(false);
            checkVerifiCode.setEnabled(true);
            checkVerifiCode.setAlpha(1);
            loginBiz.requestPwdVerCode(phoneNumberStr);
            countDownUtil = new CountDownUtil(accessVerifiCode)
                    .setCountDownMillis(60_000L)//倒计时60000ms
                    .setCountDownColor(android.R.color.white, android.R.color.white)//不同状态字体颜色
                    .start();
        } else {
            WindowToastUtils.showShort(App.getContext(), getString(R.string.phone_format_error));
        }
    }

    /**
     * 校验验证码
     */
    private void checkVerifiCodeOp() {
        if(!SystemUtil.isNetworkAvailable(App.getContext())){
            WindowToastUtils.showShort(App.getContext(), getString(R.string.tip_check_net_available));
            return;
        }
        String phoneNumberStr = phoneNumber.getText().toString().trim();
        String verifiCodeStr = verificationCode.getText().toString().trim();
        loginBiz.checkVerCode(phoneNumberStr, verifiCodeStr);
//        accessVerifiCode.setVisibility(View.GONE);
    }

    /**
     * 重置密码
     */
    private void resetPwdOp() {
        if(!SystemUtil.isNetworkAvailable(App.getContext())){
            WindowToastUtils.showShort(App.getContext(), getString(R.string.tip_check_net_available));
            return;
        }
        String inputPwdStr = inputPwd.getText().toString().trim();
        String confirmPwdStr = inputPwdAgain.getText().toString().trim();
        if("".equals(inputPwdStr) || "".equals(confirmPwdStr)){
            WindowToastUtils.showShort(App.getContext(), getString(R.string.tip_input_can_not_null));
            return;
        }else if(!inputPwdStr.equals(confirmPwdStr)){
            WindowToastUtils.showShort(App.getContext(), getString(R.string.tip_pwd_not_same));
            return;
        }
        String phoneNumberStr = phoneNumber.getText().toString().trim();
        String captcha = verificationCode.getText().toString().trim();
        loginBiz.resetPwd(phoneNumberStr, inputPwdStr, captcha);
    }

    @Override
    public void loginSuccess(CustomProfile profile) {

    }

    @Override
    public void loginFail(int type, String reason) {

    }

    @Override
    public void checkVerCode(boolean isOk) {
        if(!isResume) return;
        if(isOk){
            showWitch(RESET_PWD);
        }else {
            WindowToastUtils.showShort(App.getContext(), getString(R.string.tip_verifi_code_error));
        }
    }

    @Override
    public void resetPwd(boolean isOk, String msg) {
        if(!isResume) return;
        if(isOk){
            WindowToastUtils.showShort(App.getContext(), getString(R.string.tip_reset_pwd_succ), R.color.green_succ);
            finish();
        }
    }
}
